package com.tps.user;

import com.tps.utils.dbconn;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList; 
import javax.naming.NamingException;
  
public class usersrv {  
    public ArrayList getusers() throws ClassNotFoundException, SQLException, NamingException {  
        Connection conn = dbconn.getConnection();  
        Statement stmt = conn.createStatement();  
        ResultSet rs = stmt.executeQuery("select * from \"t_User\"");  
        ArrayList userList = null;  
        try {  
            userList = new ArrayList();  
            while (rs.next()) {  
                user ouser = new user();  
                ouser.setid(rs.getInt("FUserID"));  
                ouser.setname(rs.getString("FName"));  
                ouser.setpwd(rs.getString("FPwd"));  
                userList.add(ouser);  
            }  
            rs.close();  
            stmt.close();  
            conn.close();  
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }  
        return userList;  
    }  
  
    public void addUser(user ouser) throws ClassNotFoundException, SQLException, NamingException {  
        Connection conn = dbconn.getConnection();  
        String sql = "insert into \"t_User\" (FName,FPwd) values (?,?)";  
        try {  
            try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
                pstmt.setString(1, ouser.getname());  
                pstmt.setString(2, ouser.getpwd());  
                pstmt.executeUpdate();
            }  
            conn.close();  
        } catch (Exception e) {  
            System.out.println(e.getMessage());
        }  
    }  
  
    public void updataUser(user ouser) throws ClassNotFoundException,  
            SQLException,  
            NamingException {  
        Connection conn = dbconn.getConnection();  
        String sql = "update \"t_User\" set FName=?,FPwd=? where FUserID=?";  
        try {  
                    try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
                        pstmt.setString(1, ouser.getname());  
                        pstmt.setString(2, ouser.getpwd());  
                        pstmt.setInt(3, ouser.getid());  
                        pstmt.executeUpdate();
                    }  
            conn.close();  
        } catch (Exception e) {
            System.out.println(e.getMessage());            
        }  
    }  
  
    public void deleteUser(user ouser) throws ClassNotFoundException,  
            SQLException,  
            NamingException {  
        Connection conn = dbconn.getConnection();  
        String sql = "delete from \"t_User\" where id =?";  
        try {  
                    try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
                        pstmt.setInt(1, ouser.getid());  
                        pstmt.executeUpdate();
                    }  
            conn.close();  
        } catch (Exception e) {
            System.out.println(e.getMessage());            
        }  
    }  
}  